<?php
// **************************************************************
//  Copyright 2009 Association Management Center
//  Glenview, Illinois
//
//  File:      class_page.php
//  Author:    Mickey Freeman (mfreeman@connect2amc.com)
//  Function:  Pagination class
//
//  Created: 05/14/2009
//  Version: 1.0
//
//  Modified:
// **************************************************************

class page
{
    /**
     * SQL statement to be used by the class
     *
     * @var string
     */
    var $sql;

    /**
     * Primary ID field used to index the array returned by the function
     *
     * @var string
     */
    var $primary_key;

    /**
     * Current page identified used to determine which records
     * to pull
     *
     * @var string
     */
    var $page;

    /**
     * Items to be displayed on each page
     *
     * @var int
     */
    var $items_per_page;

    /**
     * total number of pages available for the query executed
     * calculated value used for various operations
     *
     * @var int
     */
    var $total_pages;

    /**
     * Field by which results should be sorted when pulling from the
     * database.
     *
     * @var string
     */
    var $sort_key;

    /**
     * Sort direction for sort key
     *
     * @var string
     */
    var $sort_dir;

    /**
     * boolean flag used to determine if all records should be displayed
     *
     * @var bool
     */
    var $show_all;

    /**
     * storage holder for a cound of all rows returned by sql statement
     *
     * @var int
     */
    var $items;

    /**
     * Constructor
     *
     * Initializes class for use, makes primary calculations for page number and total pages
     *
     * @param string $sql
     * @param string $primary_key
     * @param int $items_per_page
     * @param string $page
     * @param string $sort_key
     * @param string $sort_dir
     */
    function __construct($sql, $primary_key, $items_per_page = 25, $page = '', $sort_key = '', $sort_dir = 'ASC')
    {
        // load our default variables
        $this->sql = $sql;
        $this->primary_key = $primary_key;
        $this->items_per_page = $items_per_page;
        $this->page = $page;
        $this->sort_key = $sort_key;
        $this->sort_dir = $sort_dir;
        $this->show_all = false;

        //Before we do anything we need to know how much
        //data we're about to pull, so we get a row count
        $result = app_data::mysql_query($this->sql);

        $rows = mysql_num_rows($result);

        $this->items = $rows;

        // now that we have a row count we can use it to get a page count
        if ($rows % $this->items_per_page > 0) {
            $this->total_pages = intval($rows / $this->items_per_page) + 1;
        } else {
            $this->total_pages = $rows / $this->items_per_page;
        }

        if ($this->total_pages <= 0) {
            $this->total_pages = 1;
        }

        // now that we know how many pages we have, we need to know what page we're on
        if ($this->page == '') {
            $this->page = 1; // page 1 is default
        }

        if (is_numeric($this->page)) {
            if ($this->page == 0 || $this->page <= 0) {
                $this->page = 1; // if its less than 0 it needs to be 1
            }

            if ($this->page > $this->total_pages) {
                $this->page = $this->total_pages; // if its greater than our total pages then it must be last
            }
        } else {
            if ($this->page == 'first') {
                $this->page = 1; // first = page 1
            }

            if ($this->page == 'last') {
                $this->page = $this->total_pages; // last = total pages
            }

            // last if we're showing all we need to take some special steps
            if ($this->page == 'all') {
                $this->page = 1; // only one page
                $this->total_pages = 1;
                $this->show_all = true; // set our flag for later
            }
        }
    }


    /**
     * Modifies SQL statement as per the specified page, items per page, and sorting instructions and
     * executes the SQL and returns the results indexed by primary key in an array
     *
     * @return unknown
     */
    function get_page()
    {
        // if we're showing all then we need to make some adjustments
        if ($this->show_all) {
            $start = 0;
            $length = $this->items;
        } else {
            // otherwise we use our items per page to set our ranges
            $start = ($this->page - 1) * $this->items_per_page;
            $length = $this->items_per_page;
        }


        // build the new sql statement
        $sql = $this->sql . " ORDER BY " . $this->sort_key . " " . $this->sort_dir . " "
             . "LIMIT $start, $length ";

        $result = app_data::mysql_query($sql);

        $records = array();

        // grab our results
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $records[$row[$this->primary_key]] = $row;
        }

        return $records;

    }

}